<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职业名称" prop="professionName">
        <el-input
          v-model="queryParams.professionName"
          placeholder="请输入职业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否过期" prop="isExpire">
        <el-select placeholder="请选择是否过期" v-model="queryParams.isExpire" @change="handleQuery">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="认证状态" prop="status">
        <el-select placeholder="请选择认证状态" v-model="queryParams.status" @change="handleQuery">
          <el-option label="未认证" value="0"></el-option>
          <el-option label="已认证" value="1"></el-option>
          <el-option label="已过期" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
          <el-select placeholder="请选择审核状态" v-model="queryParams.auditStatus" @change="handleQuery">
            <el-option label="未审核" value="0"></el-option>
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核不通过" value="2"></el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="有效期" prop="dateValue">
        <el-date-picker
          v-model="queryParams.dateValue"
          type="daterange"
          align="right"
          size="small"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-radio-group v-model="queryParams.userType" @change="handleQuery">
          <el-radio-button :label="undefined">全部</el-radio-button>
          <el-radio-button label="20">陪诊</el-radio-button>
          <el-radio-button label="30">护工</el-radio-button>
          <el-radio-button label="40">医生</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="professionRecordList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="用户姓名" align="center" prop="nickName" />
      <el-table-column label="职业资格认证图片" align="center" prop="professionImage">
        <template slot-scope="scope">
          <el-image :src="scope.row.professionImage" style="width: 140px;height: 70px" :preview-src-list="new Array(scope.row.professionImage)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="职业名称" align="center" prop="professionName" />
      <el-table-column label="有效期" align="center" prop="validStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validStartDate, '{y}-{m}-{d}') }}-{{ parseTime(scope.row.validEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否过期" align="center" prop="validStartDate" :formatter="expireFormatter"/>
      <el-table-column label="认证状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status === '0'">未认证</span>
          <span v-if="scope.row.status === '1'">已认证</span>
          <span v-if="scope.row.status === '2'">已过期</span>
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center" prop="auditStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === '0'" style="color: #00a0e9;cursor: pointer">未审核</span>
          <span v-if="scope.row.auditStatus === '1'" style="color: green;cursor: pointer">审核通过</span>
          <span v-if="scope.row.auditStatus === '2'" style="color: red;cursor: pointer">审核失败</span>
        </template>
      </el-table-column>

      <el-table-column label="审核备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['biz:professionRecord:audit']"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户职业资质认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio v-model="form.auditStatus" label="1">审核通过</el-radio>
          <el-radio v-model="form.auditStatus" label="2">审核不通过</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark" :rules="form.auditStatus === '2' ? rules.remark : [{required: false}]">
          <el-input v-model="form.remark"  type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProfessionRecord, getProfessionRecord, delProfessionRecord, addProfessionRecord, updateProfessionRecord, changeStatus,audit } from "@/api/biz/professionRecord";

export default {
  name: "ProfessionRecord",
  data() {
    return {
      activeName:'first',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户职业资质认证表格数据
      professionRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        nickName: undefined,
        userType:undefined,
        professionImage: undefined,
        professionName: undefined,
        validStartDate: undefined,
        validEndDate: undefined,
        status: undefined,
        auditStatus: undefined,
        dateValue: undefined,
        isExpire:undefined,
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一天',
          onClick(picker) {
            const end = new Date()
            picker.$emit('pick', [end, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      // 表单参数
      form: {},
      submitLoading: false,
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "审核备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    expireFormatter(row){
      let validEndDate = row.validEndDate;
      let now = new Date();
      if(new Date(row.validEndDate) > new Date()){
        return "否"
      }else {
        return "是"
      }
    },

    /** 查询用户职业资质认证列表 */
    getList() {
      this.loading = true;
      listProfessionRecord(this.queryParams).then(response => {
        this.professionRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        professionImage: null,
        professionName: null,
        validStartDate: null,
        validEndDate: null,
        status: "0",
        auditStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户职业资质认证";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProfessionRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户职业资质认证";
      });
    },
    handleAudit(row){
      this.reset();
      this.form.id = row.id;
      this.title = "资质审核";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.auditStatus === '2' && !this.form.remark){
            this.$modal.msgError("请填写审核不通过的备注");
            return;
          }
          this.submitLoading = true
          audit(this.form).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.submitLoading = false;
            this.getList();
          }).catch(() => {
            this.submitLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户职业资质认证编号为"' + ids + '"的数据项？').then(function() {
        return delProfessionRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/professionRecord/export', {
        ...this.queryParams
      }, `professionRecord_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
